using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwordsmanCamera : BattleCamera
{
    private MBattleController m_BattleContorller;
    private MBattleController mBattleController
    {
        get
        {
            if (m_BattleContorller == null)
                m_BattleContorller = GetComponentInParent<MBattleController>();
            return m_BattleContorller;
        }
    }

    private int m_State;
    private const int State_Normal = 0;
    private const int State_End = 1;
    public const int State_Skin = 2;
    
    public Transform camPos;
    private Vector3 m_CamPosNear;
    private Quaternion m_CamRotNear;
    
    ZTransformAnim zTransformAnim;
    public ZTransformAnim TransformAnim
    {
        get
        {
            if (zTransformAnim == null) zTransformAnim = this.gameObject.AddComponent<ZTransformAnim>();
            return zTransformAnim;
        }
    }

    public Transform target;

    public Transform camPos2;
    public Transform camPosSkin;
    // Start is called before the first frame update
    void Start()
    {
        if(camPos2)
            camPos2.gameObject.SetActive(false);
        if (camPosSkin)
            camPosSkin.gameObject.SetActive(false);

        Time.fixedDeltaTime = 1 / 240f;

        CameraFitScreen(Camera);

        /*
        if (target == null)
            target = mBattleController.me.transform;
            */

        m_CamPosNear = this.camPos.localPosition;
        m_CamRotNear = this.camPos.localRotation;

        this.Camera.depthTextureMode = DepthTextureMode.Depth;
    }

    public void CameraFitScreen(Camera Camera,float rate = 1)
    {
        if (!camPos2)
            return;

        Vector3 nearPos = this.Camera.transform.localPosition;
        float ratio = (float)Screen.width / Screen.height;
        float f = (1080 / 1920f) / ratio ;
        if (f > 1)
        {
            float lerp = Mathf.Max(0, f - 1);
            Camera.transform.localPosition = Vector3.Lerp(nearPos, camPos2.localPosition, lerp);
        }
    }
    protected override void Update()
    {
        
    }
    void FixedUpdate()
    {
        Vector3 targetPos = m_CamPosNear;
        Quaternion targetRot = m_CamRotNear;

        float camPosLerp = 0.025f;

        if (m_State == State_Skin)
        {
            camPosLerp = .1f;
            targetPos = camPosSkin.localPosition;
            targetRot = camPosSkin.localRotation;
        }
        else
        {
            
        }

        if (camPos)
        {
            camPos.transform.localPosition = Vector3.Lerp(camPos.transform.localPosition, targetPos, camPosLerp);
            camPos.transform.localRotation = Quaternion.Lerp(camPos.transform.localRotation, targetRot, camPosLerp);
        }
        
        if (!target)
            return;

        Vector3 pos = target.transform.position;
        
        //pos.x = 0;
        
        transform.position = Vector3.Lerp(transform.position , pos,this.m_LerpSpeed);
    }

    public void ShowSkinCam()
    {
        if (this.camPosSkin)
        {
            this.m_State = State_Skin;
        }
    }

    public void ShowNormalCam()
    {
        this.m_State = State_Normal;
    }
}
